package de.bml.versandanzeige;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Gegenstand {

	public static final String TABLE_NAME = "gegenstaende";
	public static final String COLUMN_LABEL = "label";
	public static final String COLUMN_DETAILS = "details";
	public static final String COLUMN_TYPE = "gegenstandstyp";
	public static final String COLUMN_STANDORT = "standort";
	public static final String COLUMN_ID_EXTERN = "id_extern";

	// Datebase fields
	private static SQLiteOpenHelper dbHelper;
	private static SQLiteDatabase db;

	private String id = "";

	// Database creation sql statement
	private static final String TABLE_CREATE = "create table " + TABLE_NAME
			+ "( " 
				+ SQLiteOpenHelper.COLUMN_ID
				+ " integer primary key autoincrement, " 
				+ COLUMN_LABEL 		+ " text , " 
				+ COLUMN_DETAILS 	+ " text, "
				+ COLUMN_ID_EXTERN 	+ " text, "
				+ COLUMN_STANDORT 		+ " integer," 
				+ COLUMN_TYPE 		+ " integer not null" 
			+ ")";

	public Gegenstand(Gegenstandstyp type, String label, String details) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_LABEL, label);
		values.put(COLUMN_DETAILS, details);
		values.put(COLUMN_ID_EXTERN, "");
		values.put(COLUMN_TYPE, type.getId());
		id = Long.toString(db.insert(TABLE_NAME, null, values));
	}

	public Gegenstand(Gegenstandstyp type, String label) {
		this(type, label, "");
	}

	public Gegenstand(Gegenstandstyp type) {
		this(type, "");
	}

	public Gegenstand(long id) {
		this.id = Long.toString(id);
	}

	public long getId() {
		return Long.valueOf(id);
	}

	public static Gegenstand get(long id) {
		return new Gegenstand(id);
	}

	public String getLabel() {
		return getStringColumn(COLUMN_LABEL);
	}

	public Gegenstand setLabel(String label) {
		return setStringColumn(COLUMN_LABEL, label);
	}
	

	public Standort getStandort() {
		long id = getIntColumn(COLUMN_STANDORT);
		if(id == -1) {
			return null;
		} else {
			return Standort.get(id);
		}
	}

	public Gegenstand setStandort(Standort standort) {
		return setIntColumn(COLUMN_STANDORT, standort.getId());
	}
	
	public Gegenstandstyp getType() {
		return Gegenstandstyp.get(getIntColumn(COLUMN_TYPE));
	}

	public Gegenstand setType(Gegenstandstyp type) {
		return setIntColumn(COLUMN_LABEL, type.getId());
	}

	public String getDetails() {
		return getStringColumn(COLUMN_DETAILS);
	}

	public Gegenstand setDetails(String details) {
		return setStringColumn(COLUMN_DETAILS, details);
	}

	public String getIdExtern() {
		return getStringColumn(COLUMN_ID_EXTERN);
	}

	public Gegenstand setIdExtern(String value) {
		return setStringColumn(COLUMN_ID_EXTERN, value);
	}

	private String getStringColumn(String column) {
		Cursor c = db.query(TABLE_NAME, new String[] { column },
				SQLiteOpenHelper.COLUMN_ID + "=?", new String[] { id }, null,
				null, null);
		c.moveToFirst();
		String result = "";
		if (!c.isAfterLast()) {
			result = c.getString(0);
		}
		c.close();
		return result;
	}

	private long getIntColumn(String column) {
		Cursor c = db.query(TABLE_NAME, new String[] { column },
				SQLiteOpenHelper.COLUMN_ID + "=?", new String[] { id }, null,
				null, null);
		c.moveToFirst();
		int result = -1;
		if (!c.isAfterLast()) {
			result = c.getInt(0);
		}
		c.close();
		return result;
	}
	
	private Gegenstand setStringColumn(String column, String value) {
		ContentValues values = new ContentValues();
		values.put(column, value == null ? "" : value);
		db.update(TABLE_NAME, values, SQLiteOpenHelper.COLUMN_ID + "=?",
				new String[] { id });
		return this;
	}
	
	
	private Gegenstand setIntColumn(String column, long value) {
		ContentValues values = new ContentValues();
		values.put(column, value);
		db.update(TABLE_NAME, values, SQLiteOpenHelper.COLUMN_ID + "=?",
				new String[] { id });
		return this;
	}

	// Will be used be the ArrayAdapter of a ListView
	@Override
	public String toString() {
		return getLabel()+":"+getType().toString();
	}

	public static void onCreate(SQLiteDatabase db) {
		db.execSQL(TABLE_CREATE);
	}

	public static void onUpgrade(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

	public void delete() {
		System.out.println("Gegenstand deleted with id: " + id);
		db.delete(TABLE_NAME, SQLiteOpenHelper.COLUMN_ID + " = " + id, null);
		id = null;
	}

	public static void deleteAll() {
		System.out.println("All Gegenstand deleted.");
		db.delete(TABLE_NAME, null, null);
	}

	public static List<Gegenstand> getAll() {
		List<Gegenstand> gegenstaende = new ArrayList<Gegenstand>();

		Cursor cursor = db.query(TABLE_NAME,
				new String[] { SQLiteOpenHelper.COLUMN_ID }, null, null, null,
				null, COLUMN_LABEL);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			gegenstaende.add(new Gegenstand(cursor.getLong(0)));
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return gegenstaende;
	}

	public static List<Gegenstand> getAll(Gegenstandstyp type) {
		List<Gegenstand> gegenstaende = new ArrayList<Gegenstand>();

		Cursor cursor = db.query(TABLE_NAME,
				new String[] { SQLiteOpenHelper.COLUMN_ID }, COLUMN_TYPE + "=?",
				new String[] { type.getLabel() }, null, null, COLUMN_LABEL);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			gegenstaende.add(new Gegenstand(cursor.getLong(0)));
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return gegenstaende;
	}

	public static void reconnectDB() {
		db = dbHelper.getWritableDatabase();
	}

	public static void disconnectDB() {
		db.close();
	}

	public static void init(Context context) {
		dbHelper = new SQLiteOpenHelper(context);
		db = dbHelper.getWritableDatabase();
	}

	//
	// // public Standort getAktuellerStandort() {
	// // return aktuellerStandort;
	// // }
	// //
	// // public void setAktuellerStandort(Standort aktuellerStandort) {
	// // this.aktuellerStandort = aktuellerStandort;
	// // }

	public boolean equals(Gegenstand other) {
		if (other == null || other.id == null) {
			return false;
		} else {
			return id.equals(other.id);
		}
	}

	@Override
	public int hashCode() {
		return id.hashCode();
	}
}
